<?
class LoggerBarGraph
{
    public static function BuildGraph($node, $data)
    {
        $xml = $node->ownerDocument;

        $max = -PHP_INT_SIZE;
        foreach($data['data'] as $row)
        {
            $max = max($max, max($row['values']));
        }
        $reportGraph = $xml->createElement('report_graph');
        $node->appendChild($reportGraph);

        foreach($data['data'] as $label => $row)
        {
            $graphRow = $xml->createElement('graph_row');
            $reportGraph->appendChild($graphRow);
            $rowLabel = $xml->createElement('row_label', $label);
            $graphRow->appendChild($rowLabel);
            if (is_array($row['values']) && count($row['values']) > 0)
            {
                $values = $row['values'];
                arsort($values);
                foreach($values as $key => $value)
                {
                    $rowItem = $xml->createElement('row_item');
                    $graphRow->appendChild($rowItem);
                    $rowItem->setAttribute('value', $value);
                    if ($max)
                        $width = round(100 * $value / ($max), 1);
                    else
                        $width = 0;
                    $rowItem->setAttribute('width', $width . '%');
                    $rowItem->setAttribute('color', $data['colors'][$key]);
                }
            }
            if (is_array($row['attributes']) && count($row['attributes']) > 0)
            {
                foreach($row['attributes'] as $key => $value)
                {
                    $graphRow->setAttribute($key, $value);
                }
            }
        }
        
        foreach($data['legend'] as $key => $name)
        {
            $legendRow = $xml->createElement('legend_row', $label);
            $reportGraph->appendChild($legendRow);
            $legendRow->setAttribute('name', $name);
            $legendRow->setAttribute('color', $data['colors'][$key]);
        }
    }
}